import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_module/core/hi_flutter_bridge.dart';
import 'package:flutter_module/model/goods_model.dart';
import 'package:transparent_image/transparent_image.dart';

const GOODS_ITEM = {
  "goodsId": "1574920192239",
  "categoryId": "2",
  "hot": true,
  "sliderImages": [
    {
      "url":
          "https://o.devio.org/images/as/goods/images/2019-04-28/76fc31f8-b74b-4cd4-b262-166235b65f1c.jpg",
      "type": 1
    },
    {
      "url":
          "https://o.devio.org/images/as/goods/images/2019-04-29/85ab3987-5ad6-4def-8daa-39ad7e42f7ba.jpg",
      "type": 1
    },
    {
      "url":
          "https://o.devio.org/images/as/goods/images/2019-05-08/d58fb8f2-f233-4f6c-b14a-a0cf033c7d6f.jpg",
      "type": 1
    }
  ],
  "marketPrice": "¥79",
  "groupPrice": "38.8",
  "completedNumText": "已拼2.5万件",
  "goodsName": "2019新款ins雪纺韩版连衣裙女春夏长裙胖妹妹大码短袖中长款裙子",
  "tags": "极速退款 全场包邮 7天无理由退货 48小时发货",
  "joinedAvatars": null,
  "createTime": "2019-11-28 13:49:52",
  "sliderImage":
      "https://o.devio.org/images/as/goods/images/2019-04-28/76fc31f8-b74b-4cd4-b262-166235b65f1c.jpg"
};

class recommendItem extends StatelessWidget {
  final GoodsModel item;
  final int index;

  const recommendItem({Key key, this.item, this.index}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: () {
          //主动向native发送消息
          HiFlutterBridge.getInstance()
              .goToNative({"action": "goToDetail", "goodsId": item.goodsId});
        },
        child: Card(
          child: PhysicalModel(
            color: Colors.transparent,
            clipBehavior: Clip.antiAlias,
            borderRadius: BorderRadius.circular(5),
            child: Column(
              children: [
                _itemImage(context),
                Container(
                  padding: EdgeInsets.all(4),
                  child: Text(
                    item.goodsName,
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(fontSize: 14, color: Colors.black),
                  ),
                ),
                _infoText()
              ],
            ),
          ),
        ));
  }

  _itemImage(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return Container(
      //设置最小初始高度，防止动态图片高度时的抖动
      constraints: BoxConstraints(minHeight: size.width / 2),
      child: FadeInImage.memoryNetwork(
        placeholder: kTransparentImage,
        image: item.sliderImage,
        fit: BoxFit.cover,
      ),
    );
  }

  _infoText() {
    return Container(
      padding: EdgeInsets.fromLTRB(6, 0, 6, 10),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: EdgeInsets.only(bottom: 5),
            child: Text(
              item.tags,
              style: TextStyle(fontSize: 11, color: Colors.deepPurpleAccent),
            ),
          ),
          Row(
            children: [
              Text(
                "￥",
                style: TextStyle(fontSize: 10, color: Colors.redAccent),
              ),
              Padding(
                padding: EdgeInsets.only(right: 5),
                child: Text(
                  item.groupPrice,
                  style: TextStyle(fontSize: 18, color: Colors.redAccent),
                ),
              ),
              Text(item.completedNumText,
                  style: TextStyle(fontSize: 18, color: Colors.grey))
            ],
          )
        ],
      ),
    );
  }
}
